package File;

import java.io.*;

public class CopyFiles {
    static String srcPath = "java语法\\src\\File\\Files";
    static String purPath = "java语法\\src\\File\\copyFiles";

    public static void main(String[] args) throws IOException {
        File srcFile = new File(srcPath);
        File purFile = new File(purPath);
        copyFiles(srcFile, purFile);
    }

    public static void copyFiles(File src, File purpose) throws IOException {
        purpose.mkdirs();
        File[] files = src.listFiles();
        for (File file : files) {
            if (file.isFile()) {
                if (file.getName().matches(".*txt")) {
                    System.out.println(file.getName());
                    FileInputStream fis = new FileInputStream(file);
                    FileOutputStream fos = new FileOutputStream(new File(purpose, file.getName()));
                    byte[] bytes = new byte[1024];
                    int len;
                    while ((len = fis.read(bytes)) != -1) {
                        fos.write(bytes, 0, len);
                    }
                    fis.close();
                    fos.close();
                }
            } else {
                copyFiles(file, new File(purpose, file.getName()));
            }
        }
    }
}
